package com.drore.logapp.utils;

import android.content.Context;
import android.widget.Toast;

import com.drore.logapp.base.BaseApplication;

public class ToastUtil {

    private static Toast toast = null;

    public static final int SHOW = 0;
    public static final int LONG = 1;

    public static void show(Context context, String msg) {
        if (context != null) {
            if (toast == null) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    public static void show(String msg) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getInstance(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public static void show(Context context, String msg, int time) {
        if (context != null) {
            if (toast == null) {
                toast = Toast.makeText(context, msg, time);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    private static String oldMsg;
    protected static Toast locationToast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

    /**
     * 吐出一个显示时间较短的提示
     *
     * @param context 上下文
     * @param s       文本内容
     */
    public static void showToast(Context context, String s, int time) {
        if (locationToast == null) {
            locationToast = Toast.makeText(context, s, time);
            locationToast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > time) {
                    locationToast.show();
                }
            } else {
                oldMsg = s;
                locationToast.setText(s);
                locationToast.show();
            }
        }
        oneTime = twoTime;
    }
}
